<?php
class ldap extends control
{
    public $referer;

    /**
     * Construct 
     * 
     * @access public
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->locate(inlink('setting'));
    }

    public function setting() 
    {
        $this->view->title      = $this->lang->ldap->common . $this->lang->colon . $this->lang->ldap->setting;
        $this->view->position[] = html::a(inlink('index'), $this->lang->ldap->common);
        $this->view->position[] = $this->lang->ldap->setting;
        $this->display();
    }
    
    // 将LDAP信息保存至config.php
    public function save()
    {
        if (!empty($_POST)) {
            $this->config->ldap->host = $this->post->ldapHost;
            $this->config->ldap->version = $this->post->ldapVersion;
            $this->config->ldap->bindDN = $this->post->ldapBindDN;
            $this->config->ldap->bindPWD = $this->post->ldapPassword;
            $this->config->ldap->baseDN =  $this->post->ldapBaseDN;
            $this->config->ldap->searchFilter = $this->post->ldapFilter;
            $this->config->ldap->uid = $this->post->ldapAttr;
            $this->config->ldap->mail = $this->post->ldapMail;

            // 把配置写入配置文件
            $ldapConfig = "<?php \n"
                ."\$config->ldap = new stdclass();\n"
                ."\$config->ldap->host = '{$this->post->ldapHost}';\n"
                ."\$config->ldap->version = '{$this->post->ldapVersion}';\n"
                ."\$config->ldap->bindDN = '{$this->post->ldapBindDN}';\n"
                ."\$config->ldap->bindPWD = '{$this->post->ldapPassword}';\n"
                ."\$config->ldap->baseDN = '{$this->post->ldapBaseDN}';\n"
                ."\$config->ldap->searchFilter = '{$this->post->ldapFilter}';\n"
                ."\$config->ldap->uid = '{$this->post->ldapAttr}';\n"
                ."\$config->ldap->mail = '{$this->post->ldapMail}';\n"
                ."\$config->ldap->name = '{$this->post->ldapName}';\n";
            
            $file = fopen("config.php", "w") or die("Unable to open file!");
            fwrite($file, $ldapConfig); 
            fclose($file); 
            $this->locate(inlink('setting'));
        }
    }
	
    // 同步用户信息
    public function syncUsers()
    {
	    $this->ldap->syncUsers();
    }
}
